﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;

namespace Tool12306.Windows
{
    /// <summary>
    /// Interaction logic for InputRandCodeWindow.xaml
    /// </summary>
    public partial class InputRandCodeWindow : Window
    {
        public string RandCode { get; set; }
        private HttpClient _client;

        public InputRandCodeWindow(HttpClient client)
        {
            InitializeComponent();


            _client = client;
            this.Loaded += new RoutedEventHandler(InputRandCodeWindow_Loaded);
        }

        void InputRandCodeWindow_Loaded(object sender, RoutedEventArgs e)
        {
            _12306Proxy proxy = new _12306Proxy(_client);
            byte[] data = proxy.GetVerifyImage2();
            if (data != null)
            {
                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.StreamSource = new MemoryStream(data);
                bi.EndInit();
                this.image1.Source = bi;
            }
            else
            {
                while (data == null)
                {
                    data = proxy.GetVerifyImage2();
                    if (data != null)
                    {
                        BitmapImage bi = new BitmapImage();
                        bi.BeginInit();
                        bi.StreamSource = new MemoryStream(data);
                        bi.EndInit();
                        this.image1.Source = bi;
                        System.Threading.Thread.Sleep(200);
                    }
                }
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            RandCode = textBox1.Text;
            DialogResult = true;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _12306Proxy proxy = new _12306Proxy(_client);
            byte[] data = proxy.GetVerifyImage2();
            if (data != null)
            {
                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.StreamSource = new MemoryStream(data);
                bi.EndInit();
                this.image1.Source = bi;
            }
            else
            {
                while (data == null)
                {
                    if (data != null)
                    {
                        BitmapImage bi = new BitmapImage();
                        bi.BeginInit();
                        bi.StreamSource = new MemoryStream(data);
                        bi.EndInit();
                        this.image1.Source = bi;
                        System.Threading.Thread.Sleep(200);
                    }
                }
            }
        }
    }
}
